# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : message_mar.py
# Time       ：2025/6/3 15:27
# Description：
"""

import base64
import json
from utils.log import log

# Base64编码的JSON数据


def unmarshal_kafka(coded_data: str) -> dict:
    """解码Base64数据并解析为JSON"""
    decoded_bytes = base64.b64decode(coded_data)

    decoded_str = decoded_bytes.decode('utf-8')

    parsed_data = json.loads(decoded_str)
    log.info(parsed_data)
    return parsed_data


def marshal_kafka(coded_data: dict) -> dict:
    """将JSON数据编码为Base64字符串"""
    json_str = json.dumps(coded_data, ensure_ascii=False).encode('utf-8')

    encoded_bytes = base64.b64encode(json_str)
    data = encoded_bytes.decode('ascii')
    log.info(data)
    return data


if __name__ == '__main__':
    encoded_data = "eyJpZCI6IDUsICJwcm9qZWN0SWQiOiA3OSwgImVjbiI6ICIyMDIiLCAibGFiZWxzIjogW3siayI6ICLotYTmupDnlKjpgJQiLCAidiI6ICLorqLljZXotYTmupAifV0sICJpcEFkZHJlc3MiOiAiMTAuMTQxLjEyLjEwNCIsICJzeXN0ZW1JbWFnZSI6ICJvcy11d294eTdyd2ttNGFkeWJrMDEwZnllanFsN3A3IiwgInNwZWMiOiAiZWdjLXBjZmk1LTMwNjAtdjEtQSIsICJtdWx0aXBsZU9wZW5OdW0iOiAxfQ=="

    unmarshal_kafka(encoded_data)

    coded_data = {'id': 5, 'projectId': 79, 'ecn': '202', 'labels': [{'k': '资源用途', 'v': '订单资源'}], 'ipAddress': '10.141.12.104', 'systemImage': 'os-uwoxy7rwkm4adybk010fyejql7p7', 'spec': 'egc-pcfi5-3060-v1-A', 'multipleOpenNum': 1}
    marshal_kafka(coded_data)
